package cn.ym.mall.service.impl;

import cn.ym.mall.dao.TypeDao;
import cn.ym.mall.dao.impl.TypeDaoImpl;
import cn.ym.mall.entity.Type;
import cn.ym.mall.service.TypeService;
import cn.ym.mall.utils.DruidUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TypeServiceImpl implements TypeService {
    private TypeDao typeDao = new TypeDaoImpl();

    /**
     * 查询所有类型
     *
     * @return
     */
    @Override
    public List<Type> getAll() throws SQLException {
        Connection conn = null;
        List<Type> typeList = new ArrayList<>();
        try {
            conn = DruidUtils.getConnection();
            typeList = typeDao.getAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return typeList;
    }

    /**
     * 根据id查询类型
     *
     * @param tid
     * @return
     */
    @Override
    public Type getTypeByTid(Integer tid) throws SQLException {
        Connection conn = null;
        Type type = null;
        try {
            conn = DruidUtils.getConnection();
            type = typeDao.getTypeByTid(tid);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return type;
    }

    /**
     * 新增类型
     *
     * @param type
     * @return
     */
    @Override
    public boolean add(Type type) throws SQLException {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);
            if (typeDao.add(type) == 1) {
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        } catch (Exception e) {
            DruidUtils.rollback();
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 修改类型
     *
     * @param type
     * @return
     */
    @Override
    public boolean update(Type type) throws SQLException {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);
            if (typeDao.update(type) == 1) {
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        } catch (Exception e) {
            DruidUtils.rollback();
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 删除类型
     *
     * @param tid
     * @return
     */
    @Override
    public boolean delete(Integer tid) throws SQLException {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);
            if (typeDao.delete(tid) == 1) {
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        } catch (Exception e) {
            DruidUtils.rollback();
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return false;
    }
}
